package com.katsu.myapplib.app;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import com.katsu.myapplib.secret.Secret;
import com.katsu.myapplib.secret.SecretAESDESede;
import com.katsu.myapplib.util.UtilApp;
import com.katsu.myapplib.util.UtilMD5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@SuppressLint({ "NewApi", "CommitPrefEdits" })
public abstract class AppPreferences {

    private Map<String, String> m = new HashMap<>();

    private Secret<String, String> s;

    private SharedPreferences b;

    private SharedPreferences.Editor a;

    public AppPreferences(String name) {

        s = new SecretAESDESede(UtilMD5.MD5Encode(UtilApp.packageName(), "UTF-8"), SecretAESDESede.AES_CBC_PKCS7PADDING);

        b = AppApplication.INSTANCE.getSharedPreferences(UtilMD5.MD5Encode(name, "UTF-8"), Context.MODE_PRIVATE);

        a = b.edit();

    }

    public void putBoolean(String arg0, boolean arg1) {

        a.putBoolean(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1).commit();

    }

    public void putFloat(String arg0, float arg1) {

        a.putFloat(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1).commit();

    }

    public void putInt(String arg0, int arg1) {

        a.putInt(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1).commit();

    }

    public void putLong(String arg0, long arg1) {

        a.putLong(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1).commit();

    }

    public void putString(String arg0, String arg1) {

        try {

            m.put(arg0, arg1);

            a.putString(UtilMD5.MD5Encode(arg0, "UTF-8"), s.encrypt(arg1)).commit();

        } catch (Exception e) {}

    }

    public void putStringSet(String arg0, Set<String> arg1) {

        a.putStringSet(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1).commit();

    }

    public boolean getBoolean(String arg0, boolean arg1) {

        return b.getBoolean(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

    }

    public float getFloat(String arg0, float arg1) {

        return b.getFloat(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

    }

    public int getInt(String arg0, int arg1) {

        return b.getInt(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

    }

    public long getLong(String arg0, long arg1) {

        return b.getLong(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

    }

    public String getString(String arg0, String arg1) {

        String v = b.getString(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

        if (!v.equals(arg1)) {

            try {

                if (!m.containsKey(arg0)) {

                    m.put(arg0, s.decrypt(v));

                }

                return m.get(arg0);

            } catch (Exception e) {

                return "数据异常";

            }

        } else {

            return v;

        }

    }

    public Set<String> getStringSet(String arg0, Set<String> arg1) {

        return b.getStringSet(UtilMD5.MD5Encode(arg0, "UTF-8"), arg1);

    }

    public void putHistory(String history, int size) {

        if (history == null) {

            return;

        }

        String t = history.trim();

        if (!t.equals("")) {

            List<String> h = getHistory();

            if (h.contains(t)) {

                h.remove(t);

            }

            h.add(0, t);

            if (h.size() > size) {

                h.remove(h.size() - 1);

            }

            putString("history", h.toString().replace(" ", "").replace("[", "").replace("]", ""));

        }

    }

    public List<String> getHistory() {

        List<String> l = new ArrayList();

        try {

            String h = getString("history", "");

            if (!h.equals("")) {

                l.addAll(Arrays.asList(h.split(",")));

            }

        } catch (Exception e) {}

        return l;

    }

    public void clearHistory() {

        putStringSet("history", null);

    }

    public String getUUID() {

        String u = getString("UUID", "");

        if (u.equals("")) {

            putString("UUID", u = UUID.randomUUID().toString().replace("-", ""));

        }

        return u;

    }

    /**
     * @param history  存放的数
     * @param name  集合数据名称
     * @param size
     */
    public void putHistory(String history, String name,int size) {

        if (history == null) {

            return;

        }

        String t = history.trim();

        if (!t.equals("")) {

            List<String> h = getHistory(name);

            if (h.contains(t)) {

                h.remove(t);

            }

            h.add(0, t);

            if (h.size() > size) {

                h.remove(h.size() - 1);

            }

            putString(name, h.toString().replace(" ", "").replace("[", "").replace("]", ""));

        }

    }

    /**
     * @param name 根据名称 获取集合数据
     * @return
     */
    public List<String> getHistory(String name) {

        List<String> l = new ArrayList();

        try {

            String h = getString(name, "");

            if (!h.equals("")) {

                l.addAll(Arrays.asList(h.split(",")));

            }

        } catch (Exception e) {}

        return l;

    }

    /**
     * @param name 根据名称 获取集合数据
     */
    public void clearHistory(String name) {

        putStringSet(name, null);

    }



    public void clear() {

        a.clear().commit();

    }


}

